<?php

namespace Controller;

abstract class Base extends \Lib\Core\Controller {

	public $currentUserInfo;

	public $oAuthInfo;

	public $city;

	public function actionBefore() {
		$menu = \Entity\Menu::instance()->web;
		$this->assign('_menu', $menu);
		$this->setCurrentMenuKey('');
		$this->assign('_metaTitle', '');
		$this->assign('_metaKeywords', '');
		$this->assign('_metaDescription', '');
		$mask = $this->getUserMask();
		$this->currentUserInfo = \Business\User::instance()->getCurrentUserInfo($mask);
		if (!$this->currentUserInfo) {
			$this->oAuthInfo = \Business\User::instance()->getOAuthInfoByMask($mask);
		}
		if ($this->currentUserInfo && $this->currentUserInfo['City']) {
			$this->city = \Business\City::instance()->getCityInfoByCode($this->currentUserInfo['City']);
		} else {
			$this->city = array();
		}
		$this->uri = $_SERVER['REQUEST_URI'];
		$fromUrl = $this->getParam('fromurl');
		$fromUrl = urldecode($fromUrl);
		$fromUrl = $fromUrl ? $fromUrl : '/';
		$this->assign('_fromUrl', $fromUrl);
		$this->assign('_currentUserInfo', $this->currentUserInfo);
		$this->assign('_uri', $this->uri);
		$this->assign('_city', $this->city);
		$this->setStaticFileVersion(time());
		$this->setMeta('家有狗狗');
	}

	public function setCurrentMenuKey($key) {
		$this->assign('_currentMenuKey', strtolower($key));
	}

	public function setMeta($title, $keywords = '', $description = '') {
		$webName = '家有狗狗';
		if ($title) {
			$title = $webName . ' - ' . $title;
		} else {
			$title = $webName;
		}
		$this->assign('_metaTitle', $title);
		if ($keywords) {
			$this->assign('_metaKeywords', $keywords);
		}
		if ($description) {
			$this->assign('_metaDescription', $description);
		}
	}

	public function mustLogin() {
		if (!$this->currentUserInfo) {
			\Lib\Util\Comm::redirect('/login.html?fromurl=' . urlencode($this->uri));
		}
	}

	public function mustComplete() {
		$this->mustLogin();
		if (!$this->currentUserInfo['Username']) {
			\Lib\Util\Comm::redirect('/setting.html');
		}
	}
}